package org.myname.struts.example2.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Servlet Filter implementation class RequestAttributeFilter
 */
public class RequestAttributeFilter implements Filter {

    /**
     * Default constructor. 
     */
    public RequestAttributeFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		
		printAttribute(request, "before chain do filter");
		chain.doFilter(request, response);
		printAttribute(request, "after chain do filter");
	}
	
	private void printAttribute(ServletRequest request, String phase) {
		Enumeration<String> names = request.getAttributeNames();
		System.out.println("current phase " + phase);
		while(names.hasMoreElements()) {
			String name = names.nextElement();
			System.out.println("req attr " + name + "=" + request.getAttribute(name));
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
